package Chapter15;

import java.util.*;

/**
 * @author 杨森
 * @date 2022年10月29日 11:19
 */
public class GenericExercise {
    public static void main(String[] args) {
//        使用泛型方式给HashSet放入3个学生对象
        HashSet<Student> hashSet = new HashSet<Student>();
        hashSet.add(new Student("jack",18));
        hashSet.add(new Student("tom",28));
        hashSet.add(new Student("mary",19));

        for (Student student :hashSet) {
            System.out.println(student);
        }

//        使用泛型方式给HashMap放入3个学生对象
        HashMap<String, Student> map = new HashMap<String, Student>();
        map.put("tom",new Student("tom",28));
        map.put("jack",new Student("jack",18));
        map.put("mary",new Student("mary",38));

        Set<Map.Entry<String, Student>> entries = map.entrySet();
        Iterator<Map.Entry<String, Student>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Student> next =  iterator.next();
            System.out.println(next);
        }
    }
}

class Student{
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("Student{");
        sb.append("name='").append(name).append('\'');
        sb.append(", age=").append(age);
        sb.append('}');
        return sb.toString();
    }
}
